class Api::V1::CommentsController < Api::V1::BaseController
  def index
    @spu = Spu.find params[:spu_id]
    @comments = @spu.comments.order("created_at DESC").page(params[:page]).per(params[:per])
  end

  def create
    @comment = @current_user.comments.build comment_params
    if @comment.save
      @comment.order_item.update(is_evaluated: true)
      @comment.order_item.order.to_evaluated
    else
      render json: { result: false, message: '留言失败.' }, status: :ok
    end
  end

  def update
    @comment = Comment.find params[:id]
    if @comment.update comment_params
      
    else
      render json: { result: false, message: '更新失败.' }, status: :ok
    end
  end
  
  def destroy
    @comment = Comment.find params[:id]
    if @comment.destroy
      render json: { result: true, message: '删除成功.' }, status: :ok
    else
      render json: { result: false, message: '删除失败.' }, status: :ok
    end
  end

  private

  def comment_params
    params.require(:comment).permit(:spu_id, :content, :order_item_id, :score, :review, :hide_user, comment_pictures_ids: [])
  end
end
